import { useEffect, useMemo, useState } from 'react';
import { Button, Spin, Tabs, message, Row, Col, Menu, Form } from 'antd';
import { ReloadOutlined } from '@ant-design/icons';
import Cls from 'classnames';
import { getTenantID } from '@/utils';
import { ILoadMapTypeAll } from '@/common';

import '../common/styles.less';
import pageStyles from '@/common/pageLayout.less';
import { handlCheckManageService as purchaseSourceIntentService } from './HomeIntent/services';
// 菜单
import type { MenuProps } from 'antd';
import ScrollBarLayoutNotScroll from '@/components/ScrollBarLayoutNotScroll';
import MyIcon from '@/components/MyIcon';
import { IntentTableBody } from './HomeIntent/mviewTable/IntentTableBody';
import { IntentTableExtraView } from './HomeIntent/mviewTable/IntentTableExtraView';
export const ContractHomeView: React.FC<ContractHomesAPI.IPurchaseHomeView> = ({ curRes }) => {
  /** 样式根目录 */
  const styleFixCls = 'purchase-homesty';
  // 搜索表单
  const [searchForm] = Form.useForm();
  /** 保存数据 */
  const [manageDataList, setManageDataList] = useState<Record<string, any>[]>([]);
  // 菜单标识
  const [current, setCurrent] = useState('assetApplyStorage');
  /** 回调 */
  const homeCallback = (res: any) => {
    // console.log(res)
    if (res && res?.msgFlag) {
      setManageDataList(res?.rows);
    } else {
      message.error(res?.msg);
    }
  };

  // 资产转移 assetApplyAllot
  const { taskLoadingIntent, getTaskCheckDataIntent, reloadTaskDataIntent, dataIntent, paginationIntent } = purchaseSourceIntentService(curRes, homeCallback);

  /** 存入选中的tab标识 */
  const [tabPaneKey, setTabPaneKey] = useState<string>('1');

  /** 返回子组件 */
  const renderTabsPane = () => {
    if (current === 'assetApplyStorage') {
      return (
        <IntentTableBody
          curRes={curRes}
          tabPaneKey={tabPaneKey}
          listTableData={dataIntent}
          pageSizeObj={paginationIntent}
          getTaskCheckData={getTaskCheckDataIntent}
        />
      );
    } else if (current === 'assetApplyUes') {
      return (
        <IntentTableBody
          curRes={curRes}
          tabPaneKey={tabPaneKey}
          listTableData={dataIntent}
          pageSizeObj={paginationIntent}
          getTaskCheckData={getTaskCheckDataIntent}
        />
      );
    } else if (current === 'assetApplyAllot') {
      return (
        <IntentTableBody
          curRes={curRes}
          tabPaneKey={tabPaneKey}
          listTableData={dataIntent}
          pageSizeObj={paginationIntent}
          getTaskCheckData={getTaskCheckDataIntent}
        />
      );
    } else if (current === 'assetApplyReturn') {
      return (
        <IntentTableBody
          curRes={curRes}
          tabPaneKey={tabPaneKey}
          listTableData={dataIntent}
          pageSizeObj={paginationIntent}
          getTaskCheckData={getTaskCheckDataIntent}
        />
      );
    } else if (current === 'assetApplyHandle') {
      return (
        <IntentTableBody
          curRes={curRes}
          tabPaneKey={tabPaneKey}
          listTableData={dataIntent}
          pageSizeObj={paginationIntent}
          getTaskCheckData={getTaskCheckDataIntent}
        />
      );
    } else if (current === 'assetApplyTransfer') {
      return (
        <IntentTableBody
          curRes={curRes}
          tabPaneKey={tabPaneKey}
          listTableData={dataIntent}
          pageSizeObj={paginationIntent}
          getTaskCheckData={getTaskCheckDataIntent}
        />
      );
    }
  };

  /** tabs子页面 no*/
  const tabsItem = useMemo(() => {
    let items: any[] = [];
    if (current === 'assetApplyStorage') {
      items = [
        {
          label: '待办',
          key: '1',
          children: renderTabsPane(),
          isShow: curRes['/assetApplyStorage/dealHomePage'] || false,
        },
        {
          label: '已办',
          key: '4',
          children: renderTabsPane(),
          isShow: curRes['/assetApplyStorage/dealedHomePage'] || false,
        },
      ];
    } else if (current === 'assetApplyUes') {
      items = [
        {
          label: '待办',
          key: '1',
          children: renderTabsPane(),
          isShow: curRes['/assetApplyUes/dealHomePage'] || false,
        },
        {
          label: '已办',
          key: '4',
          children: renderTabsPane(),
          isShow: curRes['/assetApplyUes/dealedHomePage'] || false,
        },
      ];
    } else if (current === 'assetApplyAllot') {
      items = [
        {
          label: '待办',
          key: '1',
          children: renderTabsPane(),
          isShow: curRes['/assetApplyAllot/dealHomePage'] || false,
        },
        {
          label: '已办',
          key: '4',
          children: renderTabsPane(),
          isShow: curRes['/assetApplyAllot/dealedHomePage'] || false,
        },
      ];
    } else if (current === 'assetApplyReturn') {
      items = [
        {
          label: '待办',
          key: '1',
          children: renderTabsPane(),
          isShow: curRes['/assetApplyReturn/dealHomePage'] || false,
        },
        {
          label: '已办',
          key: '4',
          children: renderTabsPane(),
          isShow: curRes['/assetApplyReturn/dealedHomePage'] || false,
        },
      ];
    } else if (current === 'assetApplyHandle') {
      items = [
        {
          label: '待办',
          key: '1',
          children: renderTabsPane(),
          isShow: curRes['/assetApplyHandle/dealHomePage'] || false,
        },
        {
          label: '已办',
          key: '4',
          children: renderTabsPane(),
          isShow: curRes['/assetApplyHandle/dealedHomePage'] || false,
        },
      ];
    } else if (current === 'assetApplyTransfer') {
      items = [
        {
          label: '待办',
          key: '1',
          children: renderTabsPane(),
          isShow: curRes['/assetApplyTransfer/dealHomePage'] || false,
        },
        {
          label: '已办',
          key: '4',
          children: renderTabsPane(),
          isShow: curRes['/assetApplyTransfer/dealedHomePage'] || false,
        },
      ];
    }
    const newItems: any[] = items?.filter((item: any) => item?.isShow);
    return newItems;
  }, [manageDataList, curRes, current]);
  //当前选项的请求接口数据 loading 和 refresh
  useEffect(() => {
    searchForm.resetFields();
    if (current === 'assetApplyStorage') {
      if (tabPaneKey == '1') {
        getTaskCheckDataIntent({
          tablefields: 'asset_apply_storage',
          taskType: false,
        });
      } else if (tabPaneKey == '4') {
        getTaskCheckDataIntent({
          tablefields: 'asset_apply_storage',
          taskType: true,
        });
      }
    } else if (current === 'assetApplyUes') {
      if (tabPaneKey == '1') {
        getTaskCheckDataIntent({
          tablefields: 'asset_apply_ues',
          taskType: false,
        });
      } else if (tabPaneKey == '4') {
        getTaskCheckDataIntent({
          tablefields: 'asset_apply_ues',
          taskType: true,
        });
      }
    } else if (current === 'assetApplyAllot') {
      if (tabPaneKey == '1') {
        getTaskCheckDataIntent({
          tablefields: 'asset_apply_allot',
          taskType: false,
        });
      } else if (tabPaneKey == '4') {
        getTaskCheckDataIntent({
          tablefields: 'asset_apply_allot',
          taskType: true,
        });
      }
    } else if (current === 'assetApplyReturn') {
      if (tabPaneKey == '1') {
        getTaskCheckDataIntent({
          tablefields: 'asset_apply_return',
          taskType: false,
        });
      } else if (tabPaneKey == '4') {
        getTaskCheckDataIntent({
          tablefields: 'asset_apply_return',
          taskType: true,
        });
      }
    } else if (current === 'assetApplyHandle') {
      if (tabPaneKey == '1') {
        getTaskCheckDataIntent({
          tablefields: 'asset_apply_handle',
          taskType: false,
        });
      } else if (tabPaneKey == '4') {
        getTaskCheckDataIntent({
          tablefields: 'asset_apply_handle',
          taskType: true,
        });
      }
    } else if (current === 'assetApplyTransfer') {
      if (tabPaneKey == '1') {
        getTaskCheckDataIntent({
          tablefields: 'asset_apply_transfer',
          taskType: false,
        });
      } else if (tabPaneKey == '4') {
        getTaskCheckDataIntent({
          tablefields: 'asset_apply_transfer',
          taskType: true,
        });
      }
    }
  }, [current, tabPaneKey]);
  const tabsExtraBtns = () => {
    if (current === 'assetApplyStorage') {
      return (
        <IntentTableExtraView
          searchForm={searchForm}
          styleFixCls={styleFixCls}
          getTaskCheckData={getTaskCheckDataIntent}
          tabPaneKey={tabPaneKey}
          pageSizeObj={paginationIntent}
        />
      );
    } else if (current === 'assetApplyUes') {
      return (
        <IntentTableExtraView
          searchForm={searchForm}
          styleFixCls={styleFixCls}
          getTaskCheckData={getTaskCheckDataIntent}
          tabPaneKey={tabPaneKey}
          pageSizeObj={paginationIntent}
        />
      );
    } else if (current === 'assetApplyAllot') {
      return (
        <IntentTableExtraView
          searchForm={searchForm}
          styleFixCls={styleFixCls}
          getTaskCheckData={getTaskCheckDataIntent}
          tabPaneKey={tabPaneKey}
          pageSizeObj={paginationIntent}
        />
      );
    } else if (current === 'assetApplyReturn') {
      return (
        <IntentTableExtraView
          searchForm={searchForm}
          styleFixCls={styleFixCls}
          getTaskCheckData={getTaskCheckDataIntent}
          tabPaneKey={tabPaneKey}
          pageSizeObj={paginationIntent}
        />
      );
    } else if (current === 'assetApplyHandle') {
      return (
        <IntentTableExtraView
          searchForm={searchForm}
          styleFixCls={styleFixCls}
          getTaskCheckData={getTaskCheckDataIntent}
          tabPaneKey={tabPaneKey}
          pageSizeObj={paginationIntent}
        />
      );
    } else if (current === 'assetApplyTransfer') {
      return (
        <IntentTableExtraView
          searchForm={searchForm}
          styleFixCls={styleFixCls}
          getTaskCheckData={getTaskCheckDataIntent}
          tabPaneKey={tabPaneKey}
          pageSizeObj={paginationIntent}
        />
      );
    }
  };
  // 菜单选项
  const menuListMemo = useMemo(() => {
    let arr: MenuProps['items'] = [];
    if (curRes?.['/assetApplyStorage/dealHomePage'] || curRes?.['/assetApplyStorage/dealedHomePage']) {
      arr?.push({
        title: '验收入库',
        label: '验收入库',
        key: 'assetApplyStorage',
        icon: <MyIcon type="icon-tubiao_xuqiu" style={{ paddingRight: '5px' }} />,
      });
    }
    if (curRes?.['/assetApplyUes/dealHomePage'] || curRes?.['/assetApplyUes/dealedHomePage']) {
      arr?.push({
        label: '资产领用',
        title: '资产领用',
        key: 'assetApplyUes',
        icon: <MyIcon type="icon-biangeng1" style={{ paddingRight: '5px' }} />,
      });
    }
    if (curRes?.['/assetApplyAllot/dealHomePage'] || curRes?.['/assetApplyAllot/dealedHomePage']) {
      arr?.push({
        label: '资产转移',
        title: '资产转移',
        key: 'assetApplyAllot',
        icon: <MyIcon type="icon-solution1" style={{ paddingRight: '5px' }} />,
      });
    }
    if (curRes?.['/assetApplyReturn/dealHomePage'] || curRes?.['/assetApplyReturn/dealedHomePage']) {
      arr?.push({
        label: '资产退库',
        title: '资产退库',
        key: 'assetApplyReturn',
        icon: <MyIcon type="icon-shenqing" style={{ paddingRight: '5px' }} />,
      });
    }
    if (curRes?.['/assetApplyHandle/dealHomePage'] || curRes?.['/assetApplyHandle/dealedHomePage']) {
      arr?.push({
        label: '资产处置',
        title: '资产处置',
        key: 'assetApplyHandle',
        icon: <MyIcon type="icon-caozuorizhi" style={{ paddingRight: '5px' }} />,
      });
    }
    if (curRes?.['/assetApplyTransfer/dealHomePage'] || curRes?.['/assetApplyTransfer/dealedHomePage']) {
      arr?.push({
        label: '资产调拨',
        title: '资产调拨',
        key: 'assetApplyTransfer',
        icon: <MyIcon type="icon-guize" style={{ paddingRight: '5px' }} />,
      });
    }
    return arr;
  }, [curRes]);
  const onClick: MenuProps['onClick'] = (e) => {
    setCurrent(e.key);
  };
  return (
    <div className={Cls(`${styleFixCls}`, pageStyles['commonTabsLine'])}>
      <Row gutter={[8, 8]} style={{ height: '100%' }}>
        <Col span={4} className={styleFixCls + 'RootDiv-ColSpanA'} style={{ height: '100%' }}>
          <ScrollBarLayoutNotScroll style={{ padding: 0 }}>
            <Menu onClick={onClick} selectedKeys={[current]} mode="inline" items={menuListMemo} style={{ height: '100%' }} />
          </ScrollBarLayoutNotScroll>
        </Col>

        <Col span={20} className={styleFixCls + 'RootDiv-ColSpanA'} style={{ height: '100%' }}>
          <Spin spinning={taskLoadingIntent}>
            <Tabs
              activeKey={tabPaneKey}
              tabBarExtraContent={tabsExtraBtns()}
              items={tabsItem}
              onChange={(activeKey: string) => {
                setTabPaneKey(activeKey);
              }}
            />
          </Spin>
        </Col>
      </Row>
    </div>
  );
};
